ASP .Net Examples

aspExample.PNG
I wanted to start working with some controls in order to get a leg up for the next project. I created a page that I can later use (after more additions) for a visitor to log a computer problem. Each textbox has a RequiredFieldValidator that forces the user to fill in the textbox. The telephone and email textboxes have a RegExValidator that checks for proper formatting of the text that is entered. A ValidationSummary control that handles the submit button (runnning client-side). When the user clicks submit and there are errors a message box appears telling the user the errors on the page (picture below).

requiredField.PNG

I have added the code to further explain how each validator works.
<%@ Page Title="" Language="C#" MasterPageFile="~/Barnes.Master" AutoEventWireup="true" CodeBehind="ASPExamples.aspx.cs" Inherits="timbarnesengineering.School.ASPExamples" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div id="pageTitle">Customer Information</div>
    <br />
    <div>
        <asp:Label ID="Label1" runat="server" Text="First Name:   "></asp:Label>
        <asp:TextBox ID="firstnameTextbox" runat="server" Width="240px" ></asp:TextBox>
        <asp:RequiredFieldValidator ID="firstnameRequiredField" runat="server" ErrorMessage="Required Field" ControlToValidate="firstnameTextbox"></asp:RequiredFieldValidator>
    </div>
    <br />
    <div>
        <asp:Label ID="Label2" runat="server" Text="Last Name:   "></asp:Label>
        <asp:TextBox ID="lastnameTextbox" runat="server" Width="240px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="lastnameRequiredField" runat="server" ErrorMessage="Required Field" ControlToValidate="lastnameTextbox"></asp:RequiredFieldValidator>
    </div>
    <br />
    <div>
        <asp:Label ID="Label3" runat="server" Text="Telephone #:   "></asp:Label>
        <asp:TextBox ID="phoneTextbox" runat="server" Width="120px"></asp:TextBox>
        <asp:RegularExpressionValidator ID="phoneNumberRegEx" runat="server"
            ErrorMessage="Invalid Phone Number" ControlToValidate="phoneTextbox"
            ValidationExpression="^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$"></asp:RegularExpressionValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ErrorMessage="Required Field" ControlToValidate="phoneTextbox"></asp:RequiredFieldValidator>
    </div>
     <br />
     <div>
        <asp:Label ID="Label4" runat="server" Text="E-mail:   "></asp:Label>
        <asp:TextBox ID="emailTextbox" runat="server" Width="240px"></asp:TextBox>
        <asp:RegularExpressionValidator ID="emailRegEx" runat="server"
            ErrorMessage="Invalid Email Address" ControlToValidate="emailTextbox"
            ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b"></asp:RegularExpressionValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ErrorMessage="Required Field" ControlToValidate="emailTextbox"></asp:RequiredFieldValidator>
    </div>
    <br />
    <div>
    <asp:Button ID="submitButton" runat="server" Text="Submit"
        onclick="submitButton_Click" />
    </div>
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server"
        HeaderText="Please correct errors" ShowMessageBox="True" />
 
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="RightContent" runat="server">
</asp:Content>